// vuex 状态管理

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
	// 数据源
	state: {
		userinfo:uni.getStorageSync('USERINFO')||{},
		rx:uni.getStorageSync('RX')||{
			name: '',
			age: '',
			sex: '',
			mobile: '',
			canvas: { code: 0 }
		},
		historyList: uni.getStorageSync("__history") || [],
		searchList: uni.getStorageSync("SEARCH") || []
	},
	mutations: {
		SET_USER_INFO(state,userinfo){
			state.userinfo = userinfo
		},
		SET_HISTORY_LIST(state,history){
			state.historyList = history
		},
		CLEAR_HISTORY(state) {
			state.historyList = []
		},
		SET_RX(state,rx){
			state.rx = rx
		},
		SET_SEARCH(state,search){
			state.search = search
		},
		CLEAR_RX(state){
			state.rx = {
			name: '',
			age: '',
			sex: '',
			mobile: '',
			canvas: { code: 0 }
		}
		},
	},
	// 这里面的是vuex变量
	actions: {
		set_userinfo({commit},userinfo){
			uni.setStorageSync('USERINFO',userinfo)
			commit('SET_USER_INFO',userinfo)
		},
		set_history({commit,state},history){
			let list = state.historyList
			list.unshift(history)
			uni.setStorageSync('__history', list)
			commit('SET_HISTORY_LIST',list)
		},
		clearHistory({commit}) {
			uni.removeStorageSync('__history')
			commit('CLEAR_HISTORY')
		},
		set_rx({commit},rx){
			uni.setStorageSync('RX',rx)
			commit('SET_RX',rx)
		},
		set_search({commit},search){
			uni.setStorageSync('SEARCH',search)
			commit('SET_SEARCH',search)
		},
		clear_rx({commit}){
			uni.removeStorageSync('RX')
			commit('CLEAR_RX')
		},
	}
})

export default store